﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Biz;

namespace control.Dal
{
    class DalDonHang
    {
        internal static int Insert(control.Biz.BizDonHang bizDonHang)
        {
            try
            {
                DonHang pxh = new DonHang()
                {
                    MaDonHang=bizDonHang.Ma,
                    ThoiGian=bizDonHang.ThoiGian,
                    HoTen=bizDonHang.HoTen,
                    Email=bizDonHang.Email,
                    SoDienThoai=bizDonHang.SoDienThoai,
                    TongTien=(decimal)bizDonHang.TongTien,
                    TrangThai=1,
                    
                };
                DalCommon.TrangSucDB.DonHangs.InsertOnSubmit(pxh);
                DalCommon.TrangSucDB.SubmitChanges();
                foreach (BizChiTietDonHang item in bizDonHang.Items)
                {
                    ChiTietDonHang ct = new ChiTietDonHang()
                    {
                        IDDonHang=pxh.ID,
                        IDSanPham=item.SanPham.ID,
                        SoLuong=item.SoLuong,
                        ThanhTien=item.ThanhTien,
                    };
                    DalCommon.TrangSucDB.ChiTietDonHangs.InsertOnSubmit(ct);
                    DalCommon.TrangSucDB.SubmitChanges();
                    if (ct.ID != 0)
                    {
                        SanPham sp = DalCommon.TrangSucDB.SanPhams.Single(p => p.ID == ct.IDSanPham);
                        sp.SoLuong -= ct.SoLuong;
                        DalCommon.TrangSucDB.SubmitChanges();
                    }
                }
                TaiKhoan taikhoan = DalCommon.TrangSucDB.TaiKhoans.Single(p => p.SoTaiKhoan == bizDonHang.SoTaiKhoan);
                taikhoan.TaiKhoan1 -= bizDonHang.TongTien;
                DalCommon.TrangSucDB.SubmitChanges();
                return pxh.ID;
            }
            catch { return -1; }
        }

        internal static List<BizDonHang> GetAll()
        {
            var dhs = from dh in DalCommon.TrangSucDB.DonHangs
                      select new BizDonHang()
                      {
                          ID = dh.ID,
                          HoTen = dh.HoTen,
                          SoDienThoai = dh.SoDienThoai,
                          Email = dh.Email,
                          Ma = dh.MaDonHang,
                          ThoiGian = (DateTime)dh.ThoiGian,
                          TrangThai = dh.TrangThai,
                          Items = GetitemByID(dh.ID),
                      };
            return dhs.ToList<BizDonHang>();
        }

        internal static BizDonHang GetByID(int ID)
        {
            var dhs = from dh in DalCommon.TrangSucDB.DonHangs
                      where dh.ID==ID
                      select new BizDonHang()
                      {
                          ID = dh.ID,
                          HoTen = dh.HoTen,
                          SoDienThoai = dh.SoDienThoai,
                          Email = dh.Email,
                          Ma = dh.MaDonHang,
                          ThoiGian = (DateTime)dh.ThoiGian,
                          TrangThai = dh.TrangThai,
                          Items = GetitemByID(ID),
                      };
            return dhs.FirstOrDefault<BizDonHang>();
        }

        private static List<BizChiTietDonHang> GetitemByID(int ID)
        {
            var items = from item in DalCommon.TrangSucDB.ChiTietDonHangs
                        where item.IDDonHang == ID
                        select new BizChiTietDonHang()
                        {
                            ID = item.ID,
                            SoLuong = (int)item.SoLuong,
                            SanPham = BizSanPham.GetByID(item.IDSanPham)

                        };
            return items.ToList<BizChiTietDonHang>();
        }

        internal static bool Delete(BizDonHang bizDonHang)
        {
            try
            {
                DonHang dh = DalCommon.TrangSucDB.DonHangs.Single(p => p.ID == bizDonHang.ID);
                foreach (ChiTietDonHang item in dh.ChiTietDonHangs)
                {
                     SanPham sp = DalCommon.TrangSucDB.SanPhams.Single(p => p.ID == item.IDSanPham);
                        sp.SoLuong += item.SoLuong;
                        DalCommon.TrangSucDB.SubmitChanges();
                }
                DalCommon.TrangSucDB.ChiTietDonHangs.DeleteAllOnSubmit(dh.ChiTietDonHangs);
                DalCommon.TrangSucDB.SubmitChanges();
                DalCommon.TrangSucDB.DonHangs.DeleteOnSubmit(dh);
                DalCommon.TrangSucDB.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        internal static bool KiemTraSotk(string sotk)
        {
            try
            {
                TaiKhoan t = DalCommon.TrangSucDB.TaiKhoans.Single(p => p.SoTaiKhoan == sotk);
                return true;
            }
            catch
            { return false; }
        }

        internal static void Update(BizDonHang bizDonHang)
        {
            DonHang d = DalCommon.TrangSucDB.DonHangs.Single(p => p.ID == bizDonHang.ID);
            d.TrangThai = (int)bizDonHang.TrangThai;
            DalCommon.TrangSucDB.SubmitChanges();
        }
    }
}
